﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Reflection;
using FinPlusInterfaces;

namespace FinPlusAssembler
{
    public class Repository
    {
        public IPods Template {get; private set;}
        public string LinkedId {get; private set;}
        public string Name {get; private set;}
        public Dictionary<string, Assembly> Assemblys {get; private set;}
        public Env Env { get; private set; }
  
        //construct
        public Repository(IPods template, string name, Env env, string linkedId)
        {
            Template = template;
            Env = env;
            LinkedId  = linkedId;
            Name = name;
            Assemblys = new Dictionary<string, Assembly>();

            var control = template.Get("VersionControl");
            CheckEnv(control);
            CacheAssemblys(control);
        }

        //public
        public IPod Object(string id) { return Template.Get(id); }

        public Type Type(string value)
        {
            var n = value.Split('.');
            foreach (var type in Assemblys[n[0]].GetExportedTypes())
                if (type.Name == n[1])
                    return type;

            return null;
        }

        public void Tag(object obj)
        {
            if (!obj.GetType().IsTypeOf<IFinPlusComp>()) 
                return;

            var res = (IFinPlusComp)obj;
            res.LinkedId = LinkedId;
            res.Config = Name;
            res.Env = Env;
        }

        //private
        private void CheckEnv(IPod control)
        {
            var env = control.Get("Environment");
            var targetEnv = env.ToString().ToLower().EnumParse<Env>(Env.unknown);
            if (!targetEnv.Equals(Env))
                throw new TemplateException(string.Format("IOC target env={0} config env={1}", targetEnv.ToString(), Env.ToString())); 
        }

        private void CacheAssemblys(IPod control)
        {
            try
            {
                var assemblyInfos = control.Get("Assemblies").ToString().ToDict(',', "{}");
                if (assemblyInfos == null)
                    throw new TemplateException("IOC config missig assmbly list");

                foreach (var assemblyInfo in assemblyInfos)
                {
                    var targetAssembly = Paths.Values["FolderRoot"].GetAssembly(assemblyInfo.Key);
                    if (targetAssembly != null && targetAssembly.GetName().Version.ToString().Equals(assemblyInfo.Value))
                        Assemblys[assemblyInfo.Key] = targetAssembly;
                    else
                        throw new TemplateException(string.Format("IOC cant not find reference assembly {0} v{1}", assemblyInfo.Key, assemblyInfo.Value));
                }
            }
            catch (Exception e)
            {
                throw new TemplateException(string.Format("IOC assembly foramt error", e));
            }
        }
    }
}

























































































































































































//Copyright2013<jnrton>AllRightsReserved 
//Contact j.nrton@finpus